// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

var placeSearch, autocomplete;
var componentForm = {
    street_number: 'short_name',
//    street_address: 'long_name',
    route: 'long_name',
    locality: 'long_name',
//    sublocality: 'long_name',
//    neighborhood: 'long_name',
//    political: 'long_name',
    administrative_area_level_1: 'short_name',
//    administrative_area_level_2: 'short_name',
//    administrative_area_level_3: 'short_name',
    country: 'long_name',
    postal_code: 'short_name',
//    geocode: 'short_name',
};
var results = [];

//$(function () {
//    initialize();
//});

function initialize() {
    // Create the autocomplete object, restricting the search
    // to geographical location types.
    autocomplete = new google.maps.places.Autocomplete(
            /** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
            {types: ['geocode']});
    // When the user selects an address from the dropdown,
    // populate the address fields in the form.
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        fillInAddress();
    });
}

// [START region_fillform]
function fillInAddress() {
    // Get the place details from the autocomplete object.
    var place = autocomplete.getPlace();
    if (place.address_components) {
        for (var component in componentForm) {
            $('#Direccion_calle_1').val('');
            $('#Direccion_ciudad').val('');
            $('#Direccion_estado').val('');
            $('#Direccion_codigo_postal').val('');
            $('#Direccion_pais').val('');
//            document.getElementById(component).value = '';
//            document.getElementById(component).disabled = false;
        }
        // Get each component of the address from the place details
        // and fill the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                results.push({'key': addressType, 'value': val});
//                document.getElementById(addressType).value = val;
            }
        }
        $('#Direccion_calle_1').val(obtenerValor('route'));
        $('#Direccion_ciudad').val(obtenerValor('locality'));
        $('#Direccion_estado').val(obtenerValor('administrative_area_level_1'));
        $('#Direccion_codigo_postal').val(obtenerValor('postal_code'));
        $('#Direccion_pais').val(obtenerValor('country'));
    } else {
        alert('No existen datos');
    }
}
// [END region_fillform]

function obtenerValor(addressType) {
    var result = ''
    $.each(results, function (index, value) {
        if (addressType == value.key) {
            result = value.value;
        }
    });
    return result;
}


// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
    $('.pac-container').css('z-index', '1500');
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var geolocation = new google.maps.LatLng(
                    position.coords.latitude, position.coords.longitude);
            autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
                    geolocation));
        });
    }
}
// [END region_geolocation]